//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "DividendBarrierOption.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/Instruments/StrikedTypePayoff.h>
#include <gen/QL/Exercise.h>
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
#include <gen/QL/Payoff.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instruments/BarrierOption.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Processes;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CDividendBarrierOption::CDividendBarrierOption (QL::Instruments::Barrier::TypeEnum barrierType, Double barrier, Double rebate, Cephei::QL::Instruments::IStrikedTypePayoff^ payoff, Cephei::QL::IExercise^ exercise, Cephei::Core::IVector<DateTime>^ dividendDates, Cephei::Core::IVector<Double>^ dividends, Cephei::QL::IPricingEngine^ QL_Pricer) : CBarrierOption(CDividendBarrierOption::typeid)
{
    CStrikedTypePayoff^ _Cpayoff;
    CExercise^ _Cexercise;
    try
    {
#ifdef HANDLE
        _phDividendBarrierOption = NULL;
#endif
        QuantLib::Barrier::Type _barrierType = (QuantLib::Barrier::Type)barrierType ;
        QuantLib::Real _barrier = (QuantLib::Real)ValueHelper::Convert (barrier); //d
        QuantLib::Real _rebate = (QuantLib::Real)ValueHelper::Convert (rebate); //d
        _Cpayoff = safe_cast<CStrikedTypePayoff^> (payoff);
        _Cpayoff->Lock();
        boost::shared_ptr<QuantLib::StrikedTypePayoff>& _payoff = static_cast<boost::shared_ptr<QuantLib::StrikedTypePayoff>&> (_Cpayoff->GetShared ()); 
        _Cexercise = safe_cast<CExercise^> (exercise);
        _Cexercise->Lock();
        boost::shared_ptr<QuantLib::Exercise>& _exercise = static_cast<boost::shared_ptr<QuantLib::Exercise>&> (_Cexercise->GetShared ()); 
        dividendDates->Lock();
        INativeVector<DateTime>^ _NCIdividendDates = dividendDates->getFeature (NativeFeature::Value);
        CDateTimeVector^ _NCdividendDates = safe_cast<CDateTimeVector^>(_NCIdividendDates);
        std::vector<QuantLib::Date>& _dividendDates = static_cast<std::vector<QuantLib::Date>&> (_NCdividendDates->GetReference ());
        dividends->Lock();
        INativeVector<Double>^ _NCIdividends = dividends->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCdividends = safe_cast<CDoubleVector^>(_NCIdividends);
        std::vector<QuantLib::Real>& _dividends = static_cast<std::vector<QuantLib::Real>&> (_NCdividends->GetReference ());
        _ppDividendBarrierOption = new boost::shared_ptr<QuantLib::DividendBarrierOption> (new QuantLib::DividendBarrierOption ( _barrierType,  _barrier,  _rebate,  _payoff,  _exercise,  _dividendDates,  _dividends ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppDividendBarrierOption)->setPricingEngine (_QL_Pricer);
        SetBarrierOption (boost::dynamic_pointer_cast<QuantLib::BarrierOption> (*_ppDividendBarrierOption));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpayoff != nullptr) _Cpayoff->Unlock();
        if (_Cexercise != nullptr) _Cexercise->Unlock();
        if (dividendDates != nullptr) dividendDates->Unlock();    //not optional
        if (dividends != nullptr) dividends->Unlock();    //not optional
    }
}
Cephei::QL::Instruments::CDividendBarrierOption::CDividendBarrierOption (boost::shared_ptr<QuantLib::DividendBarrierOption>& childNative, Object^ owner) : CBarrierOption(CDividendBarrierOption::typeid)
{
#ifdef HANDLE
	_phDividendBarrierOption = NULL;
#endif
	_ppDividendBarrierOption = &childNative;
    _ppBarrierOption = new boost::shared_ptr<QuantLib::BarrierOption> (boost::dynamic_pointer_cast<QuantLib::BarrierOption> (*_ppDividendBarrierOption));
}
Cephei::QL::Instruments::CDividendBarrierOption::CDividendBarrierOption (QuantLib::DividendBarrierOption& childNative, Object^ owner) : CBarrierOption(CDividendBarrierOption::typeid)
{
#ifdef HANDLE
	_phDividendBarrierOption = NULL;
#endif
	_ppDividendBarrierOption = new boost::shared_ptr<QuantLib::DividendBarrierOption> (&childNative);
    _ppBarrierOption = new boost::shared_ptr<QuantLib::BarrierOption> (boost::dynamic_pointer_cast<QuantLib::BarrierOption> (*_ppDividendBarrierOption));
    _DividendBarrierOptionOwner = owner;
    _BarrierOptionOwner = owner;
}

Cephei::QL::Instruments::CDividendBarrierOption::CDividendBarrierOption (CDividendBarrierOption^ copy) : CBarrierOption(CDividendBarrierOption::typeid)
{
#ifdef HANDLE
	_phDividendBarrierOption = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppDividendBarrierOption = new boost::shared_ptr<QuantLib::DividendBarrierOption> (copy->GetShared());
        _ppBarrierOption = new boost::shared_ptr<QuantLib::BarrierOption> (boost::dynamic_pointer_cast<QuantLib::BarrierOption> (*_ppDividendBarrierOption));
    }
}
Cephei::QL::Instruments::CDividendBarrierOption::CDividendBarrierOption (PLATFORM::Type^ t) : CBarrierOption(CDividendBarrierOption::typeid)
{
#ifdef HANDLE
	_phDividendBarrierOption = NULL;
#endif
	if (!t->IsSubclassOf(CDividendBarrierOption::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CDividendBarrierOption::CDividendBarrierOption (QuantLib::Handle<QuantLib::DividendBarrierOption>& childNative, Object^ owner)  : CBarrierOption(CDividendBarrierOption::typeid)
{
	_phDividendBarrierOption = &childNative;
	_ppDividendBarrierOption = &static_cast<boost::shared_ptr<QuantLib::DividendBarrierOption>>(childNative.currentLink());
    _ppBarrierOption = new boost::shared_ptr<QuantLib::BarrierOption> (boost::dynamic_pointer_cast<QuantLib::BarrierOption> (*_ppDividendBarrierOption));
    _DividendBarrierOptionOwner = owner;
}
Cephei::QL::Instruments::CDividendBarrierOption::CDividendBarrierOption (QuantLib::Handle<QuantLib::DividendBarrierOption> childNative)  : CBarrierOption(CDividendBarrierOption::typeid)
{
	_phDividendBarrierOption = &childNative;
	_ppDividendBarrierOption = &static_cast<boost::shared_ptr<QuantLib::DividendBarrierOption>>(childNative.currentLink());
    _ppBarrierOption = new boost::shared_ptr<QuantLib::BarrierOption> (boost::dynamic_pointer_cast<QuantLib::BarrierOption> (*_ppDividendBarrierOption));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CDividendBarrierOption::CDividendBarrierOption (QuantLib::DividendBarrierOption childNative)  : CBarrierOption(CDividendBarrierOption::typeid)
{
#ifdef HANDLE
	_phDividendBarrierOption = NULL;
#endif
	_ppDividendBarrierOption = new boost::shared_ptr<QuantLib::DividendBarrierOption> (new QuantLib::DividendBarrierOption (childNative));
    _ppBarrierOption = new boost::shared_ptr<QuantLib::BarrierOption> (boost::dynamic_pointer_cast<QuantLib::BarrierOption> (*_ppDividendBarrierOption));
}
#endif

Cephei::QL::Instruments::CDividendBarrierOption::~CDividendBarrierOption ()
{
    if (_ppDividendBarrierOption != NULL)
    {
	    delete _ppDividendBarrierOption;
        _ppDividendBarrierOption = NULL;
    }
}
Cephei::QL::Instruments::CDividendBarrierOption::!CDividendBarrierOption ()
{
    if (_ppDividendBarrierOption != NULL)
    {
	    delete _ppDividendBarrierOption;
    }
}
QuantLib::DividendBarrierOption& Cephei::QL::Instruments::CDividendBarrierOption::GetReference ()
{
    if (_ppDividendBarrierOption == NULL) throw REFNEW NativeNullException ();
	return **_ppDividendBarrierOption;
}
boost::shared_ptr<QuantLib::DividendBarrierOption>& Cephei::QL::Instruments::CDividendBarrierOption::GetShared ()
{
    if (_ppDividendBarrierOption == NULL) throw REFNEW NativeNullException ();
	return *_ppDividendBarrierOption;
}
QuantLib::DividendBarrierOption* Cephei::QL::Instruments::CDividendBarrierOption::GetPointer ()
{
    if (_ppDividendBarrierOption == NULL) throw REFNEW NativeNullException ();
	return &**_ppDividendBarrierOption;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::DividendBarrierOption>& Cephei::QL::Instruments::CDividendBarrierOption::GetHandle ()
{
	if (_phDividendBarrierOption == NULL)
	{
		_phDividendBarrierOption = new Handle<QuantLib::DividendBarrierOption> (*_ppDividendBarrierOption);
	}
	return *_phDividendBarrierOption;
}
#endif
bool Cephei::QL::Instruments::CDividendBarrierOption::HasNative () 
{
	return (_ppDividendBarrierOption != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::IDividendBarrierOption^ Cephei::QL::Instruments::CDividendBarrierOption_Factory::Create (QL::Instruments::Barrier::TypeEnum barrierType, Double barrier, Double rebate, Cephei::QL::Instruments::IStrikedTypePayoff^ payoff, Cephei::QL::IExercise^ exercise, Cephei::Core::IVector<DateTime>^ dividendDates, Cephei::Core::IVector<Double>^ dividends, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CDividendBarrierOption ( barrierType,  barrier,  rebate,  payoff,  exercise,  dividendDates,  dividends,  QL_Pricer);
}
